var form = document.querySelector('form');
var p = form.querySelectorAll('p');
var yzm2 = form.querySelector('.yzm2');
var arr = [true, true, true, true, true];

//1.手机号
var reg = /^1\d{10}$/;
form.number.onblur = function () {
    if (reg.test(this.value)) {
        p[0].innerHTML = "&nbsp;"
        arr[0] = true;
    } else {
        p[0].innerHTML = "请输入正确的格式"
        arr[0] = false;
    }
}

//2.密码
var reg1 = /^\w{6}$/;
form.pswd.onblur = function () {
    if (reg1.test(this.value)) {
        p[1].innerHTML = "&nbsp;"
        arr[1] = true;
    } else {
        p[1].innerHTML = "请输入正确的密码格式"
        arr[1] = false;
    }
}

//3.确认密码
form.pswd1.onblur = function () {
    if (this.value == form.pswd.value) {
        p[2].innerHTML = "&nbsp;"
        arr[2] = true;
    } else {
        p[2].innerHTML = "两次密码不一致"
        arr[2] = false;
    }
}

//4.验证码
yzm2.innerHTML = getCode();
yzm2.onclick = function () {
    yzm2.innerHTML = getCode();
}

form.yzm.onblur = function () {
    if (this.value.toLocaleUpperCase() == yzm2.innerHTML.toLocaleUpperCase()) {
        p[3].innerHTML = "&nbsp;"
        arr[3] = true;
    } else {
        p[3].innerHTML = "验证码输入错误"
        arr[3] = false;
    }
}

//5.复选框
form.check.onclick = function () {
    if (checked) {
        arr[4] = true;
    } else {
        arr[4] = false;
    }
}


//注册
var zc1 = form.querySelector('.zc1');
zc1.onclick = function () {
    console.log(123);
    form.number.focus();
    form.number.blur();
    form.pswd.focus();
    form.pswd.blur();
    form.pswd1.focus();
    form.pswd1.blur();
    form.yzm.focus();
    form.yzm.blur();
    if (arr[0] && arr[1] && arr[2] && arr[3] && arr[4]) {
        var obj = {
            name: form.number.value,
            pass: form.pswd.value,
        }
        var list = JSON.parse(localStorage.getItem('user'))
        // return true;


        if (list) {
            list.push(obj);
            localStorage.setItem('user', JSON.stringify(list));
        } else {
            localStorage.setItem('user', JSON.stringify([obj]));
        }

        // 6.5 提示
        alert('注册成功了，快去登录吧');
        // 跳转到登录页面
        location.href = './login.html'; // 地址是两个html页面之间的相对路径

    }else{
        alert('输入不正确，请重新输入');
    }
    return false;
}
